import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';
import 'package:get/get.dart';
import 'package:sainbainu/common/services/user.dart';
import 'package:sainbainu/common/values/colors.dart';
import 'package:sainbainu/common/values/sizes.dart';
import 'package:sainbainu/common/widgets/widget.dart';
import 'package:wechat_assets_picker/wechat_assets_picker.dart';

import '../index.dart';

class CustomSliverHeader extends GetView<UserInfoController> {
  final VoidCallback? onBackTap;
  final VoidCallback? onCoverEditTap;
  final VoidCallback? onAvatarEditTap;

  const CustomSliverHeader({
    Key? key,
    this.onBackTap,
    this.onCoverEditTap,
    this.onAvatarEditTap,
  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return _buildSliverAppBar(
      leading: _buildBackButton(onBackTap),
      title: _buildThumbnailAvatar(),
      actions: [
        _buildEditCoverButton(onCoverEditTap),
        const SizedBox(width: 15),
      ],
      background: _buildContent(
        bgImage: _buildBgImage(),
        avatar: _buildAvatar(onAvatarEditTap),
      ),
    );
  }

  Widget _buildSliverAppBar({
    required Widget leading,
    required Widget title,
    required Widget background,
    required List<Widget> actions,
  }) {
    return Obx(
      () => SliverAppBar(
        leading: leading,
        title: title,
        actions: actions,
        pinned: true,
        stretch: true,
        backgroundColor:
            controller.state.isCollapsed.value ? AppColors.nav : AppColors.page,
        expandedHeight: controller.state.headerExpandedHeight,
        flexibleSpace: FlexibleSpaceBar(
          collapseMode: CollapseMode.pin,
          background: background,
        ),
      ),
    );
  }

  /// 返回按钮
  Widget _buildBackButton(VoidCallback? onPressed) {
    return Obx(
      () => IconButton(
        onPressed: onPressed,
        icon: Icon(
          Icons.arrow_back_ios_new_rounded,
          color:
              controller.state.isCollapsed.value ? Colors.black : Colors.white,
        ),
      ),
    );
  }

  /// 头部缩略头像
  Widget _buildThumbnailAvatar() {
    return Obx(
      () => AnimatedScale(
        scale: controller.state.isCollapsed.value ? 1 : 0,
        duration: const Duration(milliseconds: 300),
        curve: Curves.easeInOut,
        child: Container(
          width: 36,
          height: 36,
          decoration: BoxDecoration(
            border: Border.all(color: Colors.white, width: 2),
            borderRadius: BorderRadius.circular(36),
          ),
          child: ClipRRect(
            borderRadius: BorderRadius.circular(36),
            child: GetBuilder<UserInfoController>(
              id: 'avatar',
              builder: (_) => CustomImage(
                url: _.state.userProfileResponse.avatar ?? '',
                fit: BoxFit.cover,
                errorType: ImageErrorType.avatar,
              ),
            ),
          ),
        ),
      ),
    );
  }

  /// 头部背景编辑按钮
  Widget _buildEditCoverButton(VoidCallback? onPressed) {
    return SizedBox(
      width: 80,
      height: 30,
      child: ElevatedButton(
        onPressed: onPressed,
        style: ElevatedButton.styleFrom(
          backgroundColor: const Color.fromRGBO(255, 255, 255, 0.7),
          foregroundColor: Colors.white,
          padding: const EdgeInsets.symmetric(horizontal: 0),
          elevation: 0,
        ),
        child: Text(
          '编辑封面'.tr,
          style: const TextStyle(
            color: AppColors.active,
            fontSize: 14,
          ),
        ),
      ),
    );
  }

  /// 头部内容
  Widget _buildContent({
    required Widget bgImage,
    required Widget avatar,
  }) {
    return Stack(
      fit: StackFit.expand,
      children: [
        Positioned(
          left: 0,
          right: 0,
          top: 0,
          bottom: kAvatarSize / 2,
          child: bgImage,
        ),
        Align(
          alignment: Alignment.bottomCenter,
          child: avatar,
        ),
      ],
    );
  }

  /// 背景图片
  Widget _buildBgImage() {
    return GetBuilder<UserInfoController>(
      id: 'cover',
      builder: (_) => CustomImage(
        url: _.state.userProfileResponse.cover ?? '',
        width: Get.width,
        height: _.state.headerExpandedHeight,
        fit: BoxFit.cover,
      ),
    );
  }

  /// 头像
  Widget _buildAvatar(VoidCallback? onTap) {
    return GestureDetector(
      onTap: onTap,
      child: SizedBox(
        width: kAvatarSize,
        height: kAvatarSize,
        child: Stack(
          fit: StackFit.expand,
          children: [
            Container(
              width: kAvatarSize,
              height: kAvatarSize,
              decoration: BoxDecoration(
                border: Border.all(color: Colors.white, width: 2),
                borderRadius: BorderRadius.circular(kAvatarSize),
              ),
              child: ClipRRect(
                borderRadius: BorderRadius.circular(kAvatarSize),
                child: GetBuilder<UserInfoController>(
                  id: 'avatar',
                  builder: (_) => CustomImage(
                    url: _.state.userProfileResponse.avatar ?? '',
                    fit: BoxFit.cover,
                    errorType: ImageErrorType.avatar,
                  ),
                ),
              ),
            ),
            const Align(
              alignment: Alignment.bottomCenter,
              child: Padding(
                padding: EdgeInsets.only(bottom: 6),
                child: Icon(
                  Icons.edit_square,
                  color: Colors.white,
                  size: 24,
                ),
              ),
            ),
          ],
        ),
      ),
    );
  }
}
